package com.db.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class App
{
    public static void main(String[] args){
        if(args.length != 3){
            System.out.println("parameter:database type(mysql,sqlserver,oracle),tableName(),number of rows");
        }
        String dbType = args[0];
        String tableName = args[1];
        int num = Integer.parseInt(args[1]);
        if(dbType.equals("mysql")){
            insertMysql(tableName,num);
        }
        if(dbType.equals("sqlserver")){
            insertSqlserver(tableName,num);
        }
        if(dbType.equals("oracle")){
            insertOracle(tableName,num);
        }
    }


    private static void insertMysql(String tableName,int num){
        String cfn = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/company?useSSL=false";
        String user = "postgres";
        String pwd = "postgres";

        Connection con = null;
        Statement statement = null;
        try {
            Class.forName(cfn);
            con = DriverManager.getConnection(url, user, pwd);
            statement = con.createStatement();
            for(int i=0;i<num/1000;i++){
                for(int j=0;j<1000;j++){
                    if(tableName.equals("")){
                        statement.addBatch("");
                    }
                    if(tableName.equals("")){
                        statement.addBatch("");
                    }
                    if(tableName.equals("")){
                        statement.addBatch("");
                    }
                }
                statement.executeBatch();
            }
            for(int j=0;j<num%1000;j++){
                if(tableName.equals("")){
                    statement.addBatch("");
                }
                if(tableName.equals("")){
                    statement.addBatch("");
                }
                if(tableName.equals("")){
                    statement.addBatch("");
                }
            }
            statement.executeBatch();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
                if (con != null) con.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

    private static void insertSqlserver(String tableName,int num){
        String cfn = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String url = "jdbc:sqlserver://localhost:1433;DatabaseName=student";
        String user = "postgres";
        String pwd = "postgres";

        Connection con = null;
        Statement statement = null;
        try {
            Class.forName(cfn);
            con = DriverManager.getConnection(url, user, pwd);
            statement = con.createStatement();
            for(int i=0;i<num/1000;i++){
                for(int j=0;j<1000;j++){
                    if(tableName.equals("")){
                        statement.addBatch("");
                    }
                    if(tableName.equals("")){
                        statement.addBatch("");
                    }
                    if(tableName.equals("")){
                        statement.addBatch("");
                    }
                }
                statement.executeBatch();
            }
            for(int j=0;j<num%1000;j++){
                if(tableName.equals("")){
                    statement.addBatch("");
                }
                if(tableName.equals("")){
                    statement.addBatch("");
                }
                if(tableName.equals("")){
                    statement.addBatch("");
                }
            }
            statement.executeBatch();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
                if (con != null) con.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

    private static void insertOracle(String tableName,int num){
        String cfn = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@//127.0.0.1:1521/orcl";
        String user = "system";
        String pwd = "123456";

        Connection con = null;
        Statement statement = null;
        try {
            Class.forName(cfn);
            con = DriverManager.getConnection(url, user, pwd);
            statement = con.createStatement();
            for(int i=0;i<num/1000;i++){
                for(int j=0;j<1000;j++){
                    if(tableName.equals("")){
                        statement.addBatch("");
                    }
                    if(tableName.equals("")){
                        statement.addBatch("");
                    }
                    if(tableName.equals("")){
                        statement.addBatch("");
                    }
                }
                statement.executeBatch();
            }
            for(int j=0;j<num%1000;j++){
                if(tableName.equals("")){
                    statement.addBatch("");
                }
                if(tableName.equals("")){
                    statement.addBatch("");
                }
                if(tableName.equals("")){
                    statement.addBatch("");
                }
            }
            statement.executeBatch();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
                if (con != null) con.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
}
